package com.ict.web.msg.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ict.common.utils.SecurityUtils;
import com.ict.web.msg.domain.SysMsg;
import com.ict.web.msg.dto.QuerySysMsgDto;
import com.ict.web.msg.mapper.SysMsgMapper;
import com.ict.web.msg.service.ISysMsgService;
import com.ict.web.msg.vo.SysMsgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 通知Service业务层处理
 *
 * @author xc
 */
@Service
public class SysMsgServiceImpl extends ServiceImpl<SysMsgMapper, SysMsg> implements ISysMsgService
{
    @Autowired
    private SysMsgMapper sysMsgMapper;

    /**
     * 查询通知
     *
     * @param id 通知主键
     * @return 通知
     */
    @Override
    public SysMsg selectSysMsgById(Long id)
    {
        return sysMsgMapper.selectById(id);
    }

    /**
     * 查询通知列表
     *
     * @param sysMsg 通知
     * @return 通知
     */
    @Override
    public List<SysMsgVo> selectSysMsgList(QuerySysMsgDto sysMsg)
    {
       return sysMsgMapper.selectSysMsgList(sysMsg);
    }

    /**
     * 根据收件人ID查询所有未读消息数
     *
     * @param userId
     * @return 结果
     */
    @Override
    public int countByUserId(Long userId) {
        LambdaQueryWrapper<SysMsg> wrapper = Wrappers.<SysMsg>lambdaQuery()
                .eq(SysMsg::getMsgStatus, SysMsg.STATUS_NORMAL)
                .eq(SysMsg::getReadFlag, SysMsg.READ_FLAG_NORMAL);
        if (userId!=null){
            wrapper.eq(SysMsg::getReceiveUserId, userId);
        }
        return sysMsgMapper.selectCount(wrapper);
    }

    /**
     * 根据通知id修改通知状态为已读
     *
     * @param id
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateReadFlagById(String id) {
        LambdaQueryWrapper<SysMsg> wrapper = Wrappers.<SysMsg>lambdaQuery()
                .eq(SysMsg::getId, id)
                .eq(SysMsg::getMsgStatus, SysMsg.STATUS_NORMAL)
                .eq(SysMsg::getReadFlag, SysMsg.READ_FLAG_NORMAL);
        SysMsg sysMsg = sysMsgMapper.selectOne(wrapper);
        Assert.isTrue(sysMsg!=null,"数据不存在！");
        sysMsg.setReadFlag(SysMsg.READ_FLAG_UNNORMAL);
        Long userId = SecurityUtils.getUserId();
        sysMsg.setUpdateBy(String.valueOf(userId));
        sysMsg.setUpdateTime(LocalDateTime.now());
        return sysMsgMapper.updateById(sysMsg);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertList(List<SysMsg> list){
        this.saveBatch(list);
    }

    @Override
    public List<SysMsg> selectSysMsg(String createTime, String resourceType) {
        return sysMsgMapper.selectSysMsg(createTime,resourceType);
    }
    @Override
    public List<SysMsg> getByUserId(Long userId) {
        LambdaQueryWrapper<SysMsg> wrapper = Wrappers.<SysMsg>lambdaQuery()
                .eq(SysMsg::getMsgStatus, SysMsg.STATUS_NORMAL);
        if (userId!=null){
            wrapper.eq(SysMsg::getCreateBy, userId);
        }
        return sysMsgMapper.selectList(wrapper);
    }
}
